﻿using LuaInterface;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;

namespace Framework.UGUI
{
    public class ToggleChecker : Toggle
    {
        private GeneralCallBackWithParam _blockClickCallback;

        /// <summary>
        /// 拦截Toggle点击，回调到lua，由lua判断是否要触发
        /// </summary>
        /// <param name="blockClickCallback"></param>
        /// <param name="target"></param>
        /// <param name="param"></param>
        public void AddBlockClick(LuaFunction blockClickCallback, LuaTable target, object param = null)
        {
            GeneralCallBackWithParam.GetOrReuse(ref _blockClickCallback, blockClickCallback, target, param);
        }

        public void RemoveBlockClick()
        {
            GeneralCallBackWithParam.RecycleInst(ref _blockClickCallback);
        }

        /// <summary>
        /// lua端调用TriggerToggle完成toggle的变更
        /// </summary>
        public void TriggerClick()
        {
            InternalToggle();
        }

        /// <summary>
        /// React to clicks.
        /// </summary>
        [NoToLua]
        public override void OnPointerClick(PointerEventData eventData)
        {
            if (Input.touchCount > 1) return;
            if (eventData.button != PointerEventData.InputButton.Left)
                return;

            if (_blockClickCallback != null)
            {
                GeneralCallBackWithParam.Invoke(ref _blockClickCallback);
                return;
            }

            InternalToggle();
        }

        private void InternalToggle()
        {
            if (!IsActive() || !IsInteractable())
                return;

            isOn = !isOn;
        }

        protected override void OnDestroy()
        {
            base.OnDestroy();
            GeneralCallBackWithParam.RecycleInst(ref _blockClickCallback);
        }
 
        #region 重写Toggle隐藏 Checkmark 的 child Graphic（如果挂载 Mask，会多一个dc）
        protected override void Start()
        {
            base.Start();
            onValueChanged.AddListener(OnThisValueChanged);
            UpdateChildGraphic();
        }

        protected override void OnEnable()
        {
            base.OnEnable();
            UpdateChildGraphic();
        }

        private void OnThisValueChanged(bool value)
        {
            UpdateChildGraphic();
        }

        private void UpdateChildGraphic()
        {
            if (graphic == null)
                return;

            var gTransform = graphic.transform;
            int childCount = gTransform.childCount;
            for (int i = 0; i < childCount; ++i)
            {
                var child = gTransform.GetChild(i);
                var childGraphic = child.gameObject.GetComponent<Graphic>();
                if (childGraphic != null)
                {
                    if (Application.isPlaying)
                    {
                        // if (!Application.isPlaying)
                        // {
                        //     childGraphic.canvasRenderer.SetAlpha(!isOn ? 0.0f : 1f);
                        // }
                        // else
                        // {
                        childGraphic.CrossFadeAlpha(!isOn ? 0.0f : 1f, 0.0f, true);
                        // }
                    }
                }
            }
        }
        #endregion
    }
}